package com.factory.code.lib.database.biz.mongodb;


import com.factory.code.lib.database.DatabaseBehavior;
import com.factory.code.lib.database.biz.mysql.MysqlColumnModel;
import com.factory.code.lib.database.model.TableColumnModel;
import com.factory.code.lib.database.model.TableInfoModel;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * mysql database service implement
 *
 * @author yinyansheng
 */
@Component
@Scope(value = "prototype")
class MongodbDatabaseBehavior extends DatabaseBehavior {

    private static final String DATABASE_SQL = "SELECT database()";
    private static final String COLUMN_SQL = "SELECT DISTINCT * FROM information_schema.COLUMNS WHERE table_schema = ? AND table_name = ? ORDER BY ORDINAL_POSITION";
    private static final String TABLE_SQL = "SELECT TABLE_SCHEMA, TABLE_NAME, TABLE_COMMENT, CREATE_TIME FROM information_schema.tables WHERE table_schema = ?";

    @Override
    public String getDbType() {
        return "mongodb";
    }

    @Override
    public List<String> getDbDriver() {
        List<String> list = new ArrayList<>();
        list.add(".0_0. 没有数据库未集成");
        return list;
    }

    @Override
    public String getDatabaseName() {
        String schema = getJdbcTemplate().queryForObject(DATABASE_SQL, String.class);
        if (StringUtils.isEmpty(schema)) {
            throw new IllegalArgumentException("get schema failure");
        }
        return schema;
    }

    @Override
    public List<TableInfoModel> getTableAll() {
        String schema = getDatabaseName();
        List<TableInfoModel> tableModels = getJdbcTemplate().query(TABLE_SQL, new BeanPropertyRowMapper<>(TableInfoModel.class), schema);
        return tableModels;
    }

    @Override
    public List<String> listTableName() {

        return getTableAll().stream().map(TableInfoModel::getTableName).collect(Collectors.toList());
    }

    @Override
    public List<TableColumnModel> listFieldModel(String tableName) {
        String schema = getDatabaseName();
        List<MysqlColumnModel> columnModelList = getJdbcTemplate().query(COLUMN_SQL, new BeanPropertyRowMapper<>(MysqlColumnModel.class), schema, tableName);
        List<TableColumnModel> list = new ArrayList<>();
        for (MysqlColumnModel mysqlColumnModel : columnModelList) {
            TableColumnModel model = new TableColumnModel();
            model.setColumnName(mysqlColumnModel.getColumnName());
            model.setComment(mysqlColumnModel.getColumnComment());
            boolean isIdentity = "auto_increment".equalsIgnoreCase(mysqlColumnModel.getExtra());
            model.setIsIdentity(isIdentity);
            boolean isPk = "PRI".equalsIgnoreCase(mysqlColumnModel.getColumnKey());
            model.setIsPk(isPk);
            model.setDataType(mysqlColumnModel.getDataType());
            model.setMaxLength(mysqlColumnModel.getCharacterMaximumLength());
            list.add(model);
        }
        return list;
    }

    @Override
    public String getDbAttrType(String dbType) {
        return null;
    }
}
